package com.ai.api.controller;

import com.ai.bo.SmsCodeBO;
import com.ai.grace.result.GraceJSONResult;
import com.ai.grace.result.ResponseStatusEnum;
import com.ai.service.CandidateService;
import com.ai.service.InterviewRecordService;
import com.ai.service.JobService;
import com.ai.utils.RedisOperator;
import com.ai.utils.SMSUtils;
import com.ai.vo.CandidateVO;
import io.micrometer.common.util.StringUtils;
import org.simpleframework.xml.core.Validate;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.cache.CacheProperties;
import org.springframework.web.bind.annotation.*;
import wst.ai.pojo.candidate.Candidate;

import java.util.UUID;

/**
 * 候选人
 */
@RestController
@RequestMapping("login")
public class LoginController {
    @Autowired
    private RedisOperator redisOperator;
    @Autowired
    private CandidateService candidateService;
    @Autowired
    private InterviewRecordService interviewRecordService;
    @Autowired
    private JobService jobService;
    /**
     * 登录
     * @return
     * @throws Exception
     */
    @PostMapping("verify")
    public GraceJSONResult login(@Validate @RequestBody SmsCodeBO smsCodeBO) throws Exception {
//        判断验证码是否匹配
        String mobile = smsCodeBO.getMobile();
        String smsCode = smsCodeBO.getSmsCode();
        String code = redisOperator.get("MOBILE_SMSCODE" + ":" + mobile);
        if (StringUtils.isBlank(code)||!code.equalsIgnoreCase(smsCode)){
            return GraceJSONResult.errorCustom(ResponseStatusEnum.SMS_CODE_ERROR);
        }
//        根据mobile查询候选人
        Candidate candidate = candidateService.candidateExit(mobile);
        if (candidate == null){
//            为空则不是候选人
            return GraceJSONResult.errorCustom(ResponseStatusEnum.USER_INFO_NOT_EXIST_ERROR);
        }else {
//            是候选人的话,先判断有没有面试过or上次面试和本次面试有没有相隔半年以上
            boolean exit = interviewRecordService.isCandidateRecordExit(candidate.getId());
            if (exit){
                return GraceJSONResult.errorCustom(ResponseStatusEnum.USER_ALREADY_DID_INTERVIEW_ERROR);
            }
//            将token存入redis
            String token = UUID.randomUUID().toString();
            redisOperator.set("USER_TOKEN"+":"+candidate.getId(),token,3*60*60);
            CandidateVO candidateVO = new CandidateVO();
            BeanUtils.copyProperties(candidate,candidateVO);
            candidateVO.setUserToken(token);
            candidateVO.setCandidateId(candidate.getId());
//            查询岗位名称
            String jobName = jobService.getNameById(candidateVO.getJobId());
            candidateVO.setJobName(jobName);
//            用户进入面试流程后,删除验证码
            redisOperator.del("MOBILE_SMSCODE"+":"+mobile);
//            返回用户信息给前端
            return GraceJSONResult.ok(candidateVO);
        }
    }

    /**
     * 登录状态校验
     * @param mobile
     * @return
     */
    @PostMapping("getSMSCode")
    public GraceJSONResult getSMSCode(@RequestParam String mobile){
        if (StringUtils.isBlank(mobile)) return GraceJSONResult.error();
//        生成标识
        String code = (int)((Math.random()*9+1)*100000)+"";
        System.out.println(code);
        System.out.println(mobile);
        redisOperator.set("MOBILE_SMSCODE"+":"+mobile,code,10*60);
        return GraceJSONResult.ok();
    }
}
